<?php
class yd_go_sql {

    var $Host = "qdm114548194.my3w.com";
    var $Database = "qdm114548194_db";
    var $User = "qdm114548194";
    var $Password = "225800cgp";
    var $Link_id = 0;
    var $Query_id = 0;
    var $Row = array();
    var $Count = 0;
    var $Errorno = 0;

    function halt($msg) {
        printf("错误发生:<br>%s\n",$msg);
        printf('<br>Script:'.$_SERVER['SCRIPT_NAME']);
        die("<br><br>Session halted.");
// die("或因为权限不够导致错误发生，请联系网站管理员");
    }

    function connect() {
        if ($this->Link_id == 0) {
            @$this->Link_id=mysql_connect($this->Host, $this->User, $this->Password);
            if (!$this->Link_id) {
                $this->halt("connection fail");
            }
            if (!mysql_select_db($this->Database,$this->Link_id)) {
                $this->halt("can't connect ".$this->Database);
            }
        }
        mysql_query("SET NAMES 'utf8'");
    }

    function connectgbk() {
        if ($this->Link_id == 0) {
            @$this->Link_id=mysql_connect($this->Host, $this->User, $this->Password);
            if (!$this->Link_id) {
                $this->halt("connection fail");
            }
            if (!mysql_select_db($this->Database,$this->Link_id)) {
                $this->halt("can't connect ".$this->Database);
            }
        }
        mysql_query("SET NAMES 'GBK'");
    }

    function connectsp() {
        define('CLIENT_MULTI_RESULTS', 131072);
        if ($this->Link_id == 0) {
            @$this->Link_id=mysql_connect($this->Host, $this->User, $this->Password,1,CLIENT_MULTI_RESULTS);
            if (!$this->Link_id) {
                $this->halt("数据库连接失败");
            }
            if (!mysql_select_db($this->Database,$this->Link_id)) {
                $this->halt("޷无法连接".$this->Database);
            }
        }
        mysql_query("SET NAMES 'utf8'");
    }

    function query($Query_String) {
        @$this->Query_id = mysql_query($Query_String,$this->Link_id);
        if (!$this->Query_id) {
            $this->halt('MySql Error!'.$Query_String);
            exit;
        }
        return $this->Query_id;
    }

    function querysp($Query_String) {
        @$this->Query_id = mysql_query($Query_String,$this->Link_id);
        if (!$this->Query_id) {
            $this->halt('MySql Error!'.$Query_String);
            exit;
        }
        return $this->Query_id;
    }


    function fetch_array() {
        @$this->Row = mysql_fetch_array($this->Query_id);
        //  $stat = @$this->Row;
        return @$this->Row;
    }

    function fetch_assoc() {
        @$this->Row = mysql_fetch_assoc($this->Query_id);
        //  $stat = @$this->Row;
        return @$this->Row;
    }

    function fetch_arraysp() {
        @$this->Row = mysql_fetch_array($this->Query_id,MYSQL_ASSOC);
        return $this->Row;
    }

    function fetch_row (){
        @$this->Row = mysql_fetch_row($this->Query_id);
        $stat = $this->Row;
        return $stat;
    }

    function count_rows($Query_String) {
        @$res=mysql_query($Query_String,$this->Link_id);
        $count=mysql_num_rows($res);
        return $count;
    }

    function f($name) {
        return $this->Row[$name];
    }

    function free_result() {
        mysql_free_result($this->Query_id);
    }

    function lock_table($tablename) {

        $tablename=str_replace("'","",$tablename);
        $this->query("select GET_LOCK('".$tablename."',1) as klock");
        $this->fetch_array();
        if($this->f("klock") == "1") {
            return 1;
        } else {
            return 0;
        }
    }

    function unlock_table($tablename) {
        $tablename=str_replace("'","",$tablename);
        $this->query("select RELEASE_LOCK('".$tablename."') as ulock");
        $this->fetch_array();
        if($this->f("ulock") == "1") {
            return 1;
        } else {
            return 0;
        }
    }

    function error() {
        return (($this->Link_id) ? mysql_error($this->Link_id) : mysql_error());
    }

    function errno() {
        return intval(($this->Link_id) ? mysql_errno($this->Link_id) : mysql_errno());
    }
    function close() {
        return mysql_close($this->Link_id);
    }
}
?> 